﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PuanYonetimi
{
    public partial class frmCekListesi : XtraForm
    {
        public frmCekListesi()
        {
            InitializeComponent();
        }

        private void frmCekListesi_Load(object sender, EventArgs e)
        {
            Liste();
        }

        private void Liste()
        {
            using (PromoEntities ent = new PromoEntities())
            {
                var liste = (from m in ent.Cariler
                             join c in ent.IndirimCekleri on m.Kodu equals c.Cari_Kod
                             select new { Kodu = m.Kodu, Unvani = m.Adi, CekNo = c.Cek_No, BaslangicTarihi = c.Baslangic, BitisTarihi = c.Bitis, Tutari = c.Tutar, Kullanildi = c.Flag }).ToList();

                gridControl1.DataSource = liste;
                gridView1.BestFitColumns();
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
            {
                if (!Convert.ToBoolean(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Kullanildi"])))
                {
                    using (PromoEntities ent = new PromoEntities())
                    {
                        string cekno = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, gridView1.Columns["CekNo"]);
                        IndirimCek cek = ent.IndirimCekleri.Where(x => x.Cek_No == cekno).FirstOrDefault();
                        if (cek != null)
                        {
                            ent.DeleteObject(cek);
                            ent.SaveChanges();
                        }
                    }
                    Liste();
                }
                else
                {
                    MessageBox.Show("Kullanılmış Çeki Silemezsiniz !");
                }
            }
        }
    }
}
